<?php
    namespace Home\Controller;
    use Think\Controller;
    class KeshichengbenzongeController extends Controller {
    
                public function _initialize()
    {
        header("Access-Control-Allow-Origin: *");  

        header("Access-Control-Allow-Credentials: true"); 
      
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); //允许的请求类型
        
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

    }

    public function get_list()
    {
        $page = I("get.page");
        $limit = I("get.limit");
        $attr = I("get.attr");
        $begin_time = I("get.begin_time");
        $end_time = I("get.end_time");
        $type = I("get.type");
        $type_1 = I("get.type_1");
		
		$admin_uuid = I('admin_uuid');
		$admin_dept_list_str = get_user_dept_list($admin_uuid);

        if(empty($type))
        {
            $type = I("post.type");
        }

        if(empty($begin_time))
        {
            $begin_time = I("post.begin_time");
        }

        if(empty($end_time))
        {
            $end_time = I("post.end_time");
        }

        if(empty($begin_time))
        {

            $begin_time = 0;
        }
        else
        {
            $begin_time = strtotime($begin_time);
        }

        if(empty($end_time))
        {
            $end_time = 9999999999;
        }
        else
        {
            $end_time = strtotime($end_time);
        }

        if(empty($type))
        {

            $type = 1;
        }

        if(empty($type_1))
        {

            $type_1 = 1;
        }

        if($type == 1)
        {
            $keshi = "ORDERED_BY_DEPT";
        }
        else
        {
            $keshi = "PERFORMED_BY_DEPT";
        }

        if($type_1 == 1)
        {
            $tujing = "OUTP_CLASS2_INCOME";
        }
        else
        {
            $tujing = "INP_CLASS2_INCOME";
        }



        $sql = "  SELECT b.account_dept_code dept_code,
         DECODE (b.account_dept_code, '', '合计', MIN (b.account_dept_name))
            \"科室名称\",
         SUM (COSTS + COSTS_ARMYFREE) \"合计\",";
        
        $sql_list = "";

        $list = M("cbhs.cbhs_cost_item_dict")->select();
        for ($i=0; $i < count($list); $i++) 
        { 
            $sql_list .= "SUM (DECODE (a.COST_ITEM_CODE, '".$list[$i]["cost_item_code"]."', COSTS + COSTS_ARMYFREE, 0))
            \"".$list[$i]["cost_item_name"]."\" ,";
        }

        $sql_list = rtrim($sql_list,",");
        
        $sql .= $sql_list;

        $sql .= "    FROM CBHS.CBHS_DEPT_COST_DETAIL a, COMM.sys_dept_dict b
        WHERE ACCOUNTING_DATE >= $begin_time
         AND ACCOUNTING_DATE <= $end_time
		 AND b.dept_code in ($admin_dept_list_str)
         AND a.DEPT_CODE = b.dept_code
         
        
        GROUP BY CUBE (b.ACCOUNT_DEPT_CODE)
        ORDER BY b.account_dept_code";
        
        $data = M()->query($sql);

        for ($i=0; $i < count($data); $i++) 
        { 
            unset($data[$i]['dept_code']);
        }

        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = 0;
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }

    public function get_one()
    {
        $uuid = I("post.uuid");

        $info = M("cbhs.cbhs_dept_cost_detail")->where("uuid = '$uuid'")->find();
        $info["dept_date"] = date("Y-m-d H:i:s",$info["dept_date"]);
        $json["data"]   = $info;
        $json["code"]   = 0;
        $json["msg"]    = "";

        $this->ajaxReturn($json,"json");
    }

    public function save_one()
    {
        $data = I("post.");
        $uuid = $data["uuid"];
        unset($data["uuid"]);

        $result = M("cbhs.cbhs_dept_cost_detail")->where("uuid = '$uuid'")->save($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function add_one()
    {
        $data = I("post.");
        $uuid = uuid();
        
        $data["dept_date"] = time();
        $result = M("cbhs.cbhs_dept_cost_detail")->add($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "添加成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "添加失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function get_col_list()
    {
        $col = [];
        $col[] = "科室名称";
        $col[] = "合计";
        
        $list = M("cbhs.cbhs_cost_item_dict")->select();
        for ($i=0; $i < count($list); $i++) 
        { 
            $col[] = $list[$i]["cost_item_name"];
        }
        $json["data"] = $col;
        $json["code"] = 0;
        $json["count"] = 0;
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }


   }